package com.wdl.modules.myhouse.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wdl.modules.myhouse.common.MyPage;
import com.wdl.modules.myhouse.common.Result;
import com.wdl.modules.myhouse.dto.sys.SysRoleDTO;
import com.wdl.modules.myhouse.entity.sys.SysRoleEntity;
import com.wdl.modules.myhouse.service.SysRoleService;
import com.wdl.modules.myhouse.utils.util.PageUtil;
import com.wdl.modules.myhouse.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.Role;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 昏昏的君
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
@Slf4j
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;


    @PutMapping
    @ApiOperation("指定id 更新元素")
    public Result update( @RequestBody SysRoleDTO dto) {
        SysRoleEntity entity = new SysRoleEntity();
        BeanUtils.copyProperties(dto, entity);

        QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("ID", dto.getSid());
        if (roleService.update(entity,wrapper)) {
            return new Result().ok();
        }
        return new Result().error();
    }


    @DeleteMapping("/{id}")
    @ApiOperation("指定id删除元素")
    public Result<Boolean> del(@PathVariable("id") Integer id) {
        QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("ID", id);
        if (roleService.remove(wrapper)) {
            return new Result<Boolean>().ok();
        }
        return new Result<Boolean>().error();
    }


    @GetMapping("/allRole/page")
    @ApiOperation("分页查找角色")
    public Result<IPage<SysRoleEntity>> getRoleByPage(PageVO page) {
        log.info("角色分页对象", page.toString());
        return new Result<IPage<SysRoleEntity>>().ok().setData(roleService.getRoleByPage(PageUtil.pageVo2MyPage(page)));
    }

    @GetMapping("/allRole")
    @ApiOperation("获取所有角色")
    public Result<List<SysRoleDTO>> getAllRole() {
        List<SysRoleEntity> list = roleService.list();
        List<SysRoleDTO> dtos = new ArrayList<SysRoleDTO>();
        if (list != null) {
            dtos = list.stream().map(entity -> {
                SysRoleDTO dto = new SysRoleDTO();
                BeanUtils.copyProperties(entity, dto);
                return dto;
            }).collect(Collectors.toList());
        }
        return new Result<List<SysRoleDTO>>().ok().setData(dtos);
    }

    @GetMapping("/searchByKey/{key}")
    @ApiOperation("按照角色描述模糊查询")
    public Result<List<SysRoleDTO>> getRoleByKey(@PathVariable("key") String key) {
        QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(SysRoleEntity::getRoleDesc, key).or().like(SysRoleEntity::getRoleName, key);
        List<SysRoleEntity> list = roleService.list(wrapper);
        List<SysRoleDTO> dtos = new ArrayList<>();
        if (list != null) {
            dtos = list.stream().map(entity -> {
                SysRoleDTO roleDTO = new SysRoleDTO();
                BeanUtils.copyProperties(entity, roleDTO);
                return roleDTO;
            }).collect(Collectors.toList());
        }
        return new Result<List<SysRoleDTO>>().ok().setData(dtos);
    }

    @PostMapping("/save")
    @ApiOperation("新增角色")
    public Result saveRole(@RequestBody SysRoleDTO sysRoleDTO) {
        SysRoleEntity entity = new SysRoleEntity();
        BeanUtils.copyProperties(sysRoleDTO, entity);
        boolean save = roleService.save(entity);
        if (save) {
            return new Result().ok();
        } else {
            return new Result().error();
        }
    }


}
